#include "recvfilebox.h"
#include "ui_recvfilebox.h"
#include <QDebug>
RecvFileBox::RecvFileBox(QWidget *parent) :
    FileBox(parent),
    ui(new Ui::RecvFileBox)
{
    ui->setupUi(this);
    ui->btn_recvimg->setIconSize(QSize(120,120));
}

RecvFileBox::~RecvFileBox()
{
    delete ui;
}

void RecvFileBox::setAvatar(const QPixmap avatar)
{
    ui->label_recvfileavatar->setPixmap(avatar);
}
void RecvFileBox::setText(const QString text){
    filepath=text;
    QString tmp=text;
    int index=-1;
    for(int i=0;i<5;i++)
    {
        index=tmp.indexOf('/',index+1);
    }
    filename=text.mid(index+1);
    //if(!isnew)
    //   ui->label->setText(insertNewLinesForLabel(ui->label->font(),filename+"\n等待发送",ui->label->width()));
    //else
        ui->label->setText(insertNewLinesForLabel(ui->label->font(),filename,ui->label->width()));
}
void RecvFileBox::setImg(QPixmap img){
    ui->btn_recvimg->setIcon(img);
}

QSize RecvFileBox::getSize()
{
    return this->sizeHint();
}



void RecvFileBox::on_btn_recvimg_clicked()
{
    QFileInfo f(filepath);
    QDesktopServices::openUrl(f.absoluteFilePath());
}

void RecvFileBox::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton&&static_cast<QPushButton*>(childAt(event->pos()))==ui->btn_recvimg) {
        QPoint globalPos = this->mapToGlobal(event->pos());
        QMenu menu;
        // 添加菜单项...
        //QAction *open = menu.addAction("打开");
        QAction *favorite=menu.addAction("收藏");
        connect(favorite, &QAction::triggered, [=](){
            emit s_addFavorite(filepath,filename,QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
        });
        menu.exec(globalPos); // 在鼠标位置显示菜单
    }
    QWidget::mousePressEvent(event);
}
